package com.example.assetmanagement.service.impl;

import com.example.assetmanagement.domain.SystemConfig;
import com.example.assetmanagement.mapper.SystemConfigMapper;
import com.example.assetmanagement.service.SystemConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class SystemConfigServiceImpl implements SystemConfigService {

    private final SystemConfigMapper mapper;

    @Override
    public Map<String, String> getAll() {
        List<SystemConfig> list = mapper.selectAll();
        Map<String, String> map = new HashMap<>();
        for (SystemConfig cfg : list) {
            map.put(cfg.getConfigKey(), cfg.getConfigValue());
        }
        return map;
    }

    @Override
    @Transactional
    public void save(Map<String, String> configs) {
        if (configs == null || configs.isEmpty()) return;
        for (Map.Entry<String, String> e : configs.entrySet()) {
            mapper.upsert(e.getKey(), e.getValue());
        }
    }
} 